package cap.action;

import cap.bean.*;
import cap.dao.*;
import cap.dao.impl.*;
import cap.util.PageControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private CommentDao commentDao;
    private UserDao userDao;
    private ArticleDao articleDao;
    private SysCategoryDao sysCategoryDao;
    private CategoryDao categoryDao;

    public CommentServlet() {
        commentDao = new CommentDaoImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf8");
        response.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        if (("manage").equals(method)) {//博文评论管理
            int userId = Integer.parseInt(request.getParameter("userId"));
            if (userId == 0) {
                request.setAttribute("notFound", true);
            }
            String curPageStr = request.getParameter("curPage");
            int total = commentDao.getCountByUserId(userId);
            int curPage=1;
            if(curPageStr!=null){
                curPage=Integer.parseInt(curPageStr);
            }
            PageControl pc = new PageControl(curPage, total);
            List<Ucomment> cmtList = commentDao.getCommentByPageUserId(pc.getCurPage(), pc.getPageSize(), userId);
            pc.setList(cmtList);
            request.setAttribute("curPage", pc.getCurPage());
            request.setAttribute("totalPages", pc.getTotalPages());
            request.setAttribute("cmtList", pc.getList());
            request.getRequestDispatcher("front/CommentManage.jsp").forward(request,
                    response);
        } else if (("commit").equals(method)) {//提交博文评论
            int userId = Integer.parseInt(request.getParameter("userId"));
            int artId = Integer.parseInt(request.getParameter("artId"));
            String content = request.getParameter("comment_content");
            if (userId == 0) {
                response.sendRedirect("Login.jsp");
            } else {
                int res = commentDao.insertComment(userId, artId, content);
                String errorMsg = null;
                String succMsg = null;
                if (res > 0) {
                    succMsg = "评论文章成功！";
                } else {
                    errorMsg = "评论文章失败！";
                }
                request.getSession().setAttribute("successMsg", succMsg);
                request.getSession().setAttribute("errorMsg", errorMsg);
                response.sendRedirect("comment?method=post&artId=" + artId + "&userId="
                        + userId);
            }
        } else if (("delete").equals(method)) {//删除博文评论
            int userId = Integer.parseInt(request.getParameter("userId"));
            int cmtId = Integer.parseInt(request.getParameter("cmtId"));
            int res = commentDao.deleteComment(cmtId);
            if (res > 0) {
                request.getSession().setAttribute("succDeleMsg", "删除评论成功！");
            } else {
                request.getSession().setAttribute("errorDeleMsg", "删除评论失败！");
            }
            response.sendRedirect("comment?method=manage&userId=" + userId);
        } else if (("post").equals(method)) {//获取博文详情
            int artId = Integer.parseInt(request.getParameter("artId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            userDao = new UserDaoImpl();
            articleDao = new ArticleDaoImpl();
            sysCategoryDao = new SysCategoryDaoImpl();
            categoryDao = new CategoryDaoImpl();
            User u = userDao.getUserById(userId);//获取文章作者信息
            String author = u.getUserName();
            Article art = articleDao.getById(artId);//根据ID获取博文详情
            articleDao.updateCount(artId);//文章点击数+1
            int sysCgId = art.getSysCategoryId();
            int cgId = art.getCategoryId();
            SysCategory scg = sysCategoryDao.getById(sysCgId);//根据ID获取系统分类信息
            Category cg = categoryDao.getById(cgId);//根据ID获取个人分类信息
            List<Article> artList = articleDao.getBycgIdorscgId(cgId, sysCgId, artId);//相关分类的文章列表
            List<Comment> cmtList = commentDao.getAllByArtId(artId);//获取文章对应的评论
            request.setAttribute("author", author);
            request.setAttribute("art", art);
            request.setAttribute("scg", scg);
            request.setAttribute("cg", cg);
            request.setAttribute("cmtList", cmtList);
            request.setAttribute("artList", artList);
            request.getRequestDispatcher("front/post.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
